// import AuthUtils from '@/utils/auth';

class Request {
  constructor(options) {
    this.options = options || {};
  }
  request(url, options, showToast) {
    let _showToast = showToast || true;
    if (typeof showToast == 'undefined') {
      _showToast = true;
    } else {
      _showToast = showToast;
    }
    return new Promise((resolve, reject) => {
      const header = this.options.header || {};
      const baseUrl = this.options.baseUrl || '';
      const showLoading = options.showLoading || false;
      // const auth = new AuthUtils();
      // 合并option配置
      const _options = Object.assign(this.options, options);
      // 处理请求中没有覆盖新的 data bug
      _options.data = options.data;
      //合并请求头
      _options.header = Object.assign(header, options.header);
      if (showLoading) {
        uni.showLoading({ title: '加载中' });
      }
      // _options.header = { ...options.header, Authorization: auth.getToken() };
      uni.request({
        url: `${baseUrl}${url}`, //全局接口
        ..._options,
        success: (res) => {
          const { statusCode, data } = res;
          if (statusCode == 200) {
            // 业务异常
            if (data.code != 0) {
              try {
                _options.dealWitchBusiness(data, url, _showToast);
              } catch (e) {
                reject(data);
              }
            } else {
              resolve(data);
            }
          } else {
            // http状态异常
            uni.showToast({ icon: 'error', title: `${res.statusCode}` });
            reject(res);
          }
          if (showLoading) {
            uni.hideLoading();
          }
        },
        fail: (err) => {
          reject(err);
          if (showLoading) {
            uni.hideLoading();
          }
        }
      });
    });
  }

  // get请求
  getRequest(url, options, showToast) {
    return this.request(
      url,
      {
        method: 'GET',
        ...options
      },
      showToast
    );
  }
  // postR
  postRequest(url, options, showToast) {
    return this.request(
      url,
      {
        method: 'POST',
        ...options
      },
      showToast
    );
  }
  // put 请求
  putRequest(url, options, showToast) {
    return this.request(
      url,
      {
        method: 'PUT',
        ...options
      },
      showToast
    );
  }

  // delete 请求
  deleteRequest(url, options, showToast) {
    return this.request(
      url,
      {
        method: 'PUT',
        ...options
      },
      showToast
    );
  }

  // 文件上传(使用回调的方式能拿到上传的task用来处理中断上传和上传进度监听)
  upload(url, options) {
    const { filePath, name, showLoading, success, fail, complete } = options;
    const baseUrl = this.options.baseUrl || '';
    const auth = new AuthUtils();
    if (showLoading) {
      uni.showLoading({ title: '上传中,请稍后' });
    }
    return uni.uploadFile({
      header: {
        'content-type': 'multipart/form-data;charset=UTF-8',
        Authorization: auth.getToken()
      },
      url: `${baseUrl}${url}`,
      filePath: filePath,
      name: name || 'file',
      success: (res) => {
        const { data } = res;
        const _data = JSON.parse(data);
        if (success && _data.code == 0) {
          success(_data);
        } else {
          if (fail) {
            fail(_data);
          }
        }
      },
      fail: (e) => {
        if (showLoading) {
          uni.showToast({ icon: 'none', title: '上传失败,请稍后再试' });
        }
        if (fail) {
          fail(e);
        }
      },
      complete: () => {
        if (complete) {
          complete();
        }
        if (showLoading) {
          uni.hideLoading();
        }
      }
    });
  }
}
export default Request;
